#include "Pnj.h"

/* ****************** */
/* ****Claase Pnj**** */
/* ****************** */
Pnj::Pnj()
: moveAnim()
{
    name="Delacroix";
    surname="Richard";
    isMoving = false;

    imgPnjMovement.LoadFromFile("Images/zombie.png");
    movement.SetImage(imgPnjMovement);
    setImgPnjHead("Images/face.png");
    movement.SetSubRect(IntRect( 0, 0, SPRITE_WIDTH, SPRITE_HEIGHT));
    movement.SetPosition( (rand()%15)*QUAD_SIZE, (rand()%15)*QUAD_SIZE);
    canMove = true;

    caseX = getPositionX()/32;
    caseY = getPositionY()/32;
}

Pnj::Pnj(string n, string s, string f, string m)
    : moveAnim()
{
    name = n;
    surname = s;
    imgPnjHead.LoadFromFile(f);
    pnjHead.SetImage(imgPnjHead);

    imgPnjMovement.LoadFromFile(m);
    movement.SetImage(imgPnjMovement);
    movement.SetSubRect(IntRect( 0, 0, SPRITE_WIDTH, SPRITE_HEIGHT));
    isMoving = false;
    canMove = true;
}

Pnj::~Pnj()
{

}
void Pnj::setName(string n)
{
    name = n;
}
string Pnj::getName()
{
    return name;
}
Sprite Pnj::getPnjHead()
{
    return pnjHead;
}

void Pnj::setSurname(string s)
{
    surname = s;
}
string Pnj::getSurname()
{
    return surname;
}

void Pnj::setImgPnjHead(string s)
{

    imgPnjHead.LoadFromFile(s);
    pnjHead = Sprite(imgPnjHead);
}
void Pnj::setimgPnjMovement(string s)
{

    imgPnjMovement.LoadFromFile(s);
    movement = Sprite(imgPnjMovement);
    movement.SetSubRect(IntRect( 0, 0, SPRITE_WIDTH, SPRITE_HEIGHT));
}
void Pnj::setPosition(int x, int y)
{
    movement.SetPosition(x,y);
    caseX = getPositionX()/32;
    caseY = getPositionY()/32;
}
int Pnj::getPositionX()
{
    return movement.GetPosition().x;
}
int Pnj::getPositionY()
{
    return movement.GetPosition().y;
}

void* Pnj::threadRunAnim(void* pnj)
{
    Pnj* perso = reinterpret_cast<Pnj*>(pnj);
    perso->runAnim();
}

bool Pnj::askAnim(int d, bool moveOn)
{
    bool m = false;
    if(!isMoving and canMove)
    {
        if(moveOn)
        {
            moveAnim.setDirection(d);
            isMoving = true;
            pthread_create(&thread, NULL, threadRunAnim, this);
            m = true;
        }

        else
        {
            rotate(d);
            m = false;
        }
    }
    return m;
}

void Pnj::runAnim()
{
    moveAnim.playMovement(movement);
    isMoving = false;
    caseX = movement.GetPosition().x/32;
    caseY = movement.GetPosition().y/32;
}

Sprite Pnj::getPnjMovement()
{

    return movement;
}

int Pnj::getCaseX()
{
    return caseX;
}

int Pnj::getCaseY()
{
    return caseY;
}

int Pnj::getDestCaseX()
{
    int d = moveAnim.getDirection();
    if(d==0 or d==2)
        return caseX;
    else if(d==1)
        return caseX-1;
    else
        return caseX+1;
}

int Pnj::getDestCaseY()
{
    int d = moveAnim.getDirection();
    if(d==1 or d==3)
        return caseY;
    else if(d==0)
        return caseY-1;
    else
        return caseY+1;
}

 void Pnj::rotate(int d)
 {
        movement.SetSubRect(IntRect( 0, d*SPRITE_HEIGHT, SPRITE_WIDTH, (d+1)*SPRITE_HEIGHT));
        moveAnim.setDirection(d);
 }

 int Pnj::getDirection()
 {
     return moveAnim.getDirection();
 }

bool Pnj::getIsMoving()
{
    return isMoving;
}

void Pnj::setIsMoving(bool b)
{
    isMoving = b;
}

void Pnj::setHeadPosition(int i, int j)
{
    pnjHead.SetPosition(i,j);
}
